package com.axjy.item.task;

import com.axjy.comm.pojo.TpCinema;
import com.axjy.comm.pojo.TpImage;
import com.axjy.comm.pojo.TpItem;
import com.axjy.comm.pojo.TpItemType;
import com.axjy.item.feign.ImageFeignClient;
import com.axjy.item.mapper.CinemaMapper;
import com.axjy.item.mapper.ItemMapper;
import com.axjy.item.mapper.ItemTypeMapper;
import com.axjy.item.vo.FloorKeyVo;
import com.axjy.item.vo.ItemVo;
import org.redisson.api.RLock;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class FloorItemAndDetailItemTask {

    @Resource
    private RedissonClient redissonClient;

    @Resource
    private ItemTypeMapper itemTypeMapper;

    @Resource
    private ItemMapper itemMapper;

    @Resource
    private CinemaMapper cinemaMapper;

    @Resource
    private ImageFeignClient imageFeignClient;




    private void executeTask(){
        RLock lock = redissonClient.getLock("executeTask");
        lock.lock();
        try {
            RMap<FloorKeyVo, List<ItemVo>> map = redissonClient.getMap("tp:item:floor");
            List<TpItemType> tpItemTypes = itemTypeMapper.findByKey();
            for (TpItemType itemType : tpItemTypes) {
                FloorKeyVo floorKeyVo = new FloorKeyVo();
                floorKeyVo.setFloor(Integer.parseInt(itemType.getKey()));
                floorKeyVo.setItemTypeId(itemType.getId());
                floorKeyVo.setItemTypeName(itemType.getAliasName());
                List<TpItem> list = itemMapper.find4Floor(floorKeyVo.getItemTypeId(), 5);

                List<ItemVo> itemVoList = list.parallelStream().map(tpItem -> {

                    ItemVo itemVo = new ItemVo();
                    BeanUtils.copyProperties(tpItem, itemVo);
                    TpCinema tpCinema = cinemaMapper.selectById(tpItem.getCinemaId());
                    itemVo.setCinemaId(tpCinema.getId());
                    BeanUtils.copyProperties(tpCinema, itemVo);//注意tpCinema也有一个id
                    itemVo.setId(tpItem.getId());
                    TpImage tpImage = imageFeignClient.findByTargetId(tpItem.getId(), 2, 1);
                    if (tpImage != null) {
                        itemVo.setImgUrl(tpImage.getImgUrl());
                    } else {
                        itemVo.setImgUrl("https://img.alicdn.com/bao/uploaded/i4/2251059038/O1CN01ArR8H82GdSKGEEIC2_!!2-item_pic.png_q60.jpg_.webp");
                    }
                    return itemVo;
                }).collect(Collectors.toList());
                map.put(floorKeyVo, itemVoList);
            }
        }finally {
            lock.unlock();
        }

    }

    @Scheduled(initialDelay = 1000,fixedDelay = Long.MAX_VALUE)
    public void initialTask(){
        executeTask();
    }

    @Scheduled(cron = "0 0 2 * * ?")
    public void cronTask(){
        executeTask();
    }



}
